package br.com.raxataxi.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.raxataxi.excecoes.CPF_existem_exception;
import br.com.raxataxi.excecoes.Cpf_invalido_exception;
import br.com.raxataxi.excecoes.Id_nao_encontrado_exception;
import br.com.raxataxi.excecoes.Login_existem_exception;
import br.com.raxataxi.fachada.Fachada;
import br.com.raxataxi.modelo.Endereco;
import br.com.raxataxi.modelo.Usuario;

/**
 * Servlet implementation class ServletControleResponsavel
 */
@WebServlet("/ServletControleResponsavel")
public class ServletControleUsuario extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletControleUsuario() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Usuario a = new Usuario();
		Endereco end = new Endereco();
		Fachada teste = Fachada.getInstancia();
		String acao = request.getParameter("acao");

		if (acao.equals("salvar")) {

				try {
					
					a.setNome(request.getParameter("nome").trim());
					a.setCpf(request.getParameter("cpf").trim());

					SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");  
					a.setDataCadastro(format.parse(request.getParameter("dataCadastro").trim()));
					
					a.setEmail(request.getParameter("email").trim());
					a.setFone(request.getParameter("fone").trim());
					a.setQualificacao(Integer.parseInt(request.getParameter("qualificacao").trim()));
					
					end.setRua(request.getParameter("rua").trim());
					end.setBairro(request.getParameter("bairro").trim());
					end.setCidade(request.getParameter("cidade").trim());
					end.setEstado(request.getParameter("estado").trim());
					end.setCep(request.getParameter("cep").trim());
					end.setEndUsuario(a);
					
					a.setUsuarioEndereco(end);

					System.out.println("Saiu do bloco de login");
					a.setLogin(request.getParameter("login").trim());
					a.setSenha(request.getParameter("senha").trim());
					a.setLembreteSenha(request.getParameter("confirma").trim());
					
					teste.salvarUsuarios(a);;
					
					response.sendRedirect("/Light/secretaria/cadastro_responsavel.jsp?mensagem=salvo");

				} catch (Cpf_invalido_exception e) {
					// TODO Auto-generated catch block
					response.sendRedirect("/Light/secretaria/cadastro_responsavel.jsp?mensagem=cpfInvalido");
					e.printStackTrace();
				}
				catch (CPF_existem_exception e) {
					// TODO Auto-generated catch block
					response.sendRedirect("/Light/secretaria/cadastro_responsavel.jsp?mensagem=cpfExistente");
					e.printStackTrace();
				}
				catch (Login_existem_exception e) {
					// TODO Auto-generated catch block
					response.sendRedirect("/Light/secretaria/cadastro_responsavel.jsp?mensagem=loginExistente");
					e.printStackTrace();
				}
				catch (Exception e) {
					// TODO Auto-generated catch block
					RequestDispatcher dispatcher = request.getRequestDispatcher("/Light/secretaria/cadastro_responsavel.jsp?mensagem=salvo");
					request.setAttribute("Responsavel",a);
					dispatcher.forward(request, response);
					e.printStackTrace();
				}

			} else if (acao.equals("editar")) {
				try {
				

					teste.atualizarUsuario(a);;
					
					response.sendRedirect("/Light/secretaria/home_secretaria.jsp?mensagem=salvo");

				} catch (Id_nao_encontrado_exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		
			}
		
	}
}
